home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C20 / RawStorageIterator.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  885 b   |  31 lines

  1. //: C20:RawStorageIterator.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Demonstrate the raw_storage_iterator
  7. #include "Noisy.h"
  8. #include <iostream>
  9. #include <iterator>
  10. #include <algorithm>
  11. using namespace std;
  12.  
  13. int main() {
  14.   const int quantity = 10;
  15.   // Create raw storage and cast to desired type:
  16.   Noisy* np = 
  17.     (Noisy*)new char[quantity * sizeof(Noisy)];
  18.   raw_storage_iterator<Noisy*, Noisy> rsi(np);
  19.   for(int i = 0; i < quantity; i++)
  20.     *rsi++ = Noisy(); // Place objects in storage
  21.   cout << endl;
  22.   copy(np, np + quantity,
  23.     ostream_iterator<Noisy>(cout, " "));
  24.   cout << endl;
  25.   // Explicit destructor call for cleanup:
  26.   for(int j = 0; j < quantity; j++)
  27.     (&np[j])->~Noisy();
  28.   // Release raw storage:
  29.   delete (char*)np;
  30. } ///:~
  31.